//author-szy
$(function(){ 
	showCondition(true);//加载查询条件
	//获取前台传来的参数
	var param = drgUI.setAjaxData(tabId);
	paramTabPage[tabId].depSelect = [];
	var drgCode = param.pardrgCode ;
	var intervel = param.intervel ;
	var sliderVal = param.sliderVal ;
	var methodType= param.methodType ; //计算区间方法类型
	var dep="all"; //科室
	//加载第一个表格的标题
	$("#"+tabId+" #paygroup_drgsOnetitle").html("");
	$("#"+tabId+" #paygroup_drgsOnetitle").html(drgCode+"病组各科室内病例次均费用、费用构成及其对比情况表");
	//加载对比情况表
	progressPG_OneTable(drgCode,intervel,sliderVal,methodType,progressPG_TwoTable);
	//加载出院病例明细
	//progressPG_TwoTable(drgCode,intervel,sliderVal,methodType,"all");
	
})
//加载病组对比情况表
function progressPG_OneTable(drgCode,intervel,sliderVal,methodType,callback){
	var param=drgUI.setAjaxData(tabId);
	param.pType = "1" ;
	param.isGetCc = "0" ;
	param.monitorType = "hosp"  ;
	param.typeOne = intervel ;
	param.typeTwo = methodType ;
	if(methodType=="1"){
		param.sliderVal = (Number(sliderVal)+100)/100 ;
	}
	$.post(contextPath + "/payGroup/queryCostCaseCompare.dhc", param).done(function(data) {
		var data=data.data;
		//将获取的字符串进行排序
		data = data.sort(order("LEV"))
		//获取全院所有病例数以展示各科室的病例比	
		var allIncnt=0;
		//科室串的拼接
		paramTabPage[tabId].depSelect.push("all");
		//将下表中的科室条件加上
		$.each(data, function(i, obj) {
			if(obj.LEV == "2"){
				allIncnt=obj.INCNT;
			}
			else if(obj.LEV == "1")
			{
				paramTabPage[tabId].depSelect.push(obj.DEPNAME);
			}
		});
		progressPG_TwoTable(drgCode,intervel,sliderVal,methodType,"all");//加载全部出院病例明细	
		//将对比表赋值展现
		var columns=[
		               	{"data": "DEPNAME","title":"" },
		               	{"data": "INCNT","title":"病例数",
		               		"render": function(obj, type, full) {
		           				if(full.DEPNAME=="标杆值"){
		           					return "——" ;
		           				} else{
		           					return "<a drgCode=\""+drgCode+"\" intervel=\""+intervel+"\" sliderVal=\""+sliderVal+"\" DEPDESC=\""+full.DEPNAME+"\"  class=\"INCNT\">"+full.INCNT+"</a>" ;
		           				}
		           			}	
		               	},
		               	{"data": "","title":"病例数占比(%)",
		           			"render": function(obj, type, full) {
		           				if(full.DEPNAME=="合计") {
		           					return "100.00";
		           				} else if(full.DEPNAME=="标杆值"){
		           					return "——" ;
		           				} else{
		           					return ((full.INCNT/allIncnt)*100).toFixed(2) ;
		           				}
		           			}	
		           		},
		               	{"data": "AVGFEE","title":"次均费用" ,
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.AVGFEE,2) ;
		           			}
		           		},
		           		{"data": "DRUGCOST","title":"药品费用",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.DRUGCOST,2) ;
		           			}
		           		},
		           		{"data": "DRUGCOSTRADIO","title":"药占比(%)",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.DRUGCOSTRADIO,2) ;
		           			}
		           		},
		           		{"data": "AVGDAYS","title":"平均住院日",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.AVGDAYS,2) ;
		           			}
		           		},
		           		{"data": "MED","title":"医疗费用",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.MED,2) ;
		           			}
		           		},
		           		{"data": "NURS","title":"护理费用",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.NURS,2) ;
		           			}
		           		},
		           		{"data": "MANAGE","title":"管理费用",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.InCaseCnt,2) ;
		           			}
		           		},
		           		{"data": "TECH","title":"医技费用",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.TECH,2) ;
		           			}
		           		},
		           		{"data": "DRUG","title":"药品耗材费用",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.DRUG,2) ;
		           			}
		           		} ,
		           		{"data": "","title":"操作",
		           			"render": function(obj, type, full,meta) {
		           				if(full.DEPNAME=="标杆值"){
		           					return "" ;
		           				} else{
		           					return "<a data-rowindex='"+meta.row+"' class = 'costStructure'>费用构成</a>" ;
		           				}
		           			}
		           		}
	  ];
		var paramsDataTables={searching:false,ordering:false,stateSave:true,tableTitle: $("#"+tabId+" #paygroup_drgsOnetitle").text() ,columnDefs:[{"orderable": false,"targets": [ 0 ] }]};
		var table = $("#"+tabId+" #PG_OneTable").dataTableBasic(columns,data,paramsDataTables) ;
		//点击对照表病例数下方的表格跟着变
		$("#"+tabId+" #PG_OneTable").on("click",".INCNT",function() {
			var DEPDESC = $(this).attr("DEPDESC") ;
			if(DEPDESC=="合计" || DEPDESC=="标杆值"){
				DEPDESC="all";
			}
			progressPG_TwoTable(drgCode,intervel,sliderVal,methodType,DEPDESC);
			dep="all";
		})
		$("#"+tabId+" #PG_OneTable tr").on("click",".costStructure",function() {
			var index = $(this).attr("data-rowindex");
	    	var a = table.row( index ).data();//本期|同期|上期
	    	var b = table.row(0).data();//标杆
	    	var c = ["DEPNAME","MED","NURS","MANAGE","TECH","DRUG","AVGFEE"];
	    	costStructure(a,b,c);
		})
	})
}

//加载出院病例明细表
function progressPG_TwoTable(drgCode,intervel,sliderVal,methodType,depName){
	var param=drgUI.setAjaxData(tabId);
	param.pType = "1" ;
	param.isGetCc = "0" ;
	param.monitorType = "hosp"  ;
	param.typeOne = intervel ;
	param.typeTwo = methodType ;
	param.typeThree = depName ;
	if(methodType=="1"){
		param.sliderVal = (Number(sliderVal)+100)/100 ;
	}
	$.post(contextPath + "/payGroup/queryCostOutCaseList.dhc", param).done(function(data) {
		var data=data.data;
		var columns=[
		               	{"data": "MRNO","title":"病案号",
		               		"render": function(obj, type, full){
		    				 return "<a href=\"javascript:medicalRecord('"+full.PMAININDEX+"','"+full.NAME+"')\">"+full.MRNO+"</a>";
		    			   } 
		               	},
		               	{"data": "NAME","title":"姓名" },
		               	{"data": "GENDER","title":"性别",
		           			"render": function(obj, type, full) {
		           				if(full.GENDER=="1") {
		           					return "男";
		           				} else if(full.GENDER=="0"){
		           					return "女" ;
		           				} else{
		           					return full.GENDER;
		           				}
		           			}	
		           		},
		               	{"data": "AGE","title":"年龄" },
		           		{"data": "INDATE","title":"入院时间"},
		           		{"data": "OUTDATE","title":"出院时间"},
		           		{"data": "OUTDEP","title":"出院科室"},
		           		{"data": "DR","title":"医师"},
		           		{"data": "DIAG","title":"主要诊断编码及描述"},
		           		{"data": "OPER","title":"主要手术/操作编码及描述"},
		           		{"data": "COST","title":"住院费用",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.COST,2) ;
		           			}
		           		},
		           		{"data": "COSTF","title":"住院费用标杆",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.COSTF,2) ;
		           			}
		           		},
		           		{"data": "INDAYS","title":"住院天数"},
		           		{"data": "INDAYSF","title":"住院日标杆"},
		           		{"data": "MED","title":"医疗费用",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.MED,2) ;
		           			}
		           		},
		           		{"data": "MEDF","title":"医疗费用标杆",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.MEDF,2) ;
		           			}
		           		},
		           		{"data": "NURS","title":"护理费用",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.NURS,2) ;
		           			}
		           		},
		           		{"data": "NURSF","title":"护理费用标杆",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.NURSF,2) ;
		           			}
		           		},
		           		{"data": "MANAGE","title":"管理费用",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.MANAGE,2) ;
		           			}
		           		},
		           		{"data": "MANAGEF","title":"管理费用标杆",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.MANAGEF,2) ;
		           			}
		           		},
		           		{"data": "TECH","title":"医技费用",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.TECH,2) ;
		           			}
		           		},
		           		{"data": "TECHF","title":"医技费用标杆",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.TECHF,2) ;
		           			}
		           		},
		           		{"data": "DRUG","title":"药品耗材费用",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.DRUG,2) ;
		           			}
		           		},
		           		{"data": "DRUGF","title":"药品耗材费用标杆",
		           			"render": function(obj, type, full) {
		           				return formatNumPayGroup(full.DRUGF,2) ;
		           			}
		           		}
	  ];
		var paramsDataTables={searching:true,ordering:true,stateSave:true,scrollX:true,tableTitle: $("#"+tabId+" #paygroup_drgsTwotitle").text() ,columnDefs:[{"orderable": false,"targets": [ 0 ] }],fixedColumns:{'leftColumns' : 7}};
		//使表头只展示一行
		$("#"+tabId+" #PG_TwoTable").dataTableBasic(columns,data,paramsDataTables) ;
		//加载下拉框
		if(depName =="all"){
			$('#'+tabId+' #PG_TwoTable_filter').prepend("<label>科室过滤:<select class='select' id='PG_DepSelector' style='height:30px;'></select></label>");
			//添加数据	
			$("#"+tabId+" #PG_DepSelector").html("");
			for(var i=0;i<paramTabPage[tabId].depSelect.length;i++){
				if(paramTabPage[tabId].depSelect[i]=="all"){
					$("#"+tabId+" #PG_DepSelector").append('<option value="'+paramTabPage[tabId].depSelect[i]+'">&nbsp;全部&nbsp;</option>');
				}else{
					$("#"+tabId+" #PG_DepSelector").append('<option value="'+paramTabPage[tabId].depSelect[i]+'">'+paramTabPage[tabId].depSelect[i]+'</option>');
				}
			}
		} 
		
		//科室增加点击事件
		$("#"+tabId+" #PG_DepSelector").unbind();
		$("#"+tabId+" #PG_DepSelector").on('change',function(){
			   progressPG_TwoTable(drgCode,intervel,sliderVal,methodType,$("#"+tabId+" #PG_DepSelector").val());
		});
	})
}
